#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define pb push_back
using namespace std;
const int N = 2e5 + 3;
int rd(){
    char c = getchar();
    int res = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)){
        res = res * 10 + c - '0';
        c = getchar();
    }
    return res;
}
int dp[2][N][2];
int mx[N][2];
int s[N],n;
vector<int>a[1000002];
void chk(int&x,int y){
    if(x<y) x = y;
}
void solve(){
    memset(dp,0xcf,sizeof dp);
    n = rd();
    for(int i=1;i<=n;++i) s[i] = rd();
    a[s[1]].pb(1);
    dp[1][0][0] = dp[1][0][1] = 0;
    memset(mx,0xcf,sizeof mx);
    mx[0][1] = mx[0][0] = 0;
    for(int i=2;i<=n;++i){
        memset(dp[i%2],0xcf,sizeof dp[i%2]);
        for(int j=0;j<i;++j){
            for(int t=0;t<=1;++t){
                if(j!=i-1) chk(dp[i%2][j][t],dp[1^i%2][j][t]);
                if(j==i-1) chk(dp[i%2][j][t],mx[i-2][1^t]);
                for(auto c:a[s[i]]){
                    if(i-1==c && j<c) chk(dp[i%2][j][t],dp[1^i%2][j][t]+s[i]);
                    if(c<i-1 && j==i-1) chk(dp[i%2][j][t],dp[1^i%2][c][1^t]+s[i]);
                }
            }
        }
        a[s[i]].push_back(i);
        memset(mx,0xcf,sizeof mx);
        for(int t=0;t<=1;++t){
            for(int j=0;j<i;++j){
                mx[j][t] = dp[i%2][j][t];
                if(j) mx[j][t] = max(mx[j][t],mx[j-1][t]);
            }
        }
    }
    int ans = 0;
    for(int i=0;i<n;++i) ans = max(ans,max(dp[n%2][i][0],dp[n%2][i][1]));
    cout<<ans<<'\n';
    for(int i=1;i<=n;++i) a[s[i]].clear();
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T = rd();
    while(T--) solve();
    return 0;
}
